package com.chinadci.smartcommunitysystembackstage.controller;


import com.chinadci.smartcommunitysystembackstage.entity.ScsUser;
import com.chinadci.smartcommunitysystembackstage.service.ScsActivityService;
import com.chinadci.smartcommunitysystembackstage.service.ScsOwnerService;
import com.chinadci.smartcommunitysystembackstage.service.ScsUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Code Duck
 * @since 2021-04-13
 */
@RestController
@Api("建业E家")
@CrossOrigin
public class ScsUserController {
    @Autowired
    private ScsUserService service;

    @Autowired
    private ScsOwnerService scs;
    //查找所有用户
    @ApiOperation(value = "查询所有用户")           //swagger
    @ApiImplicitParam(name = "name",value = "用户姓名")       //表明要传入的参数
    @RequestMapping(value = "/api/useridfind",method = RequestMethod.GET)
    public Map UserFind(String name){
        Map<String,Object>map=new HashMap<>();
        map.put("data",service.UserFind(name));
        return map;
    }

    //修改用户信息
    @ApiOperation(value = "修改用户信息")           //swagger
    @ApiImplicitParam(name = "scsuser",value = "对象")       //表明要传入的参数
    @RequestMapping(value = "/api/userupdate",method = RequestMethod.POST)
    public Map UserFind(ScsUser user){
        Map<String,Object>map=new HashMap<>();
        map.put("data",service.Update(user));
        return map;
    }

    //修改账号信息时查询
    @ApiOperation(value = "修改账号信息时查询")           //swagger
    @ApiImplicitParam(name = "id",value = "账号信息编号")       //表明要传入的参数
    @RequestMapping(value = "/api/zhfind",method = RequestMethod.POST)
    public Map Find(int id){
        Map<String,Object>map=new HashMap<>();
        map.put("data",service.Find(id));
        return map;
    }

    //删除用户账号信息
    @ApiOperation(value = "删除用户账号信息")           //swagger
    @ApiImplicitParam(name = "scsuser",value = "对象")       //表明要传入的参数
    @RequestMapping(value = "/api/userdel",method = RequestMethod.POST)
    public Map UserFind(int id){
        Map<String,Object>map=new HashMap<>();
        map.put("data",service.Del(id));
        return map;
    }

    //新增用户账号信息
    @ApiOperation(value = "新增用户账号信息")           //swagger
    @ApiImplicitParam(name = "scsuser",value = "对象")       //表明要传入的参数
    @RequestMapping(value = "/api/userinsert",method = RequestMethod.POST)
    public Map Insert(ScsUser user){
        Map<String,Object>map=new HashMap<>();
        String phone=user.getUPhone();
        if (scs.Finds(phone)==null){
            map.put("phone",0);     //0代表此电话号码在用户信息没有找到
            return map;
        }
        if (service.Find(phone)!=null){
            map.put("phone",1);     //1代表此电话号码在用户账号信息已经注册过
            return map;
        }
        Integer oid=scs.Findss(phone).getOId();
        user.setOId(oid);
        map.put("data",service.Insert(user));
        map.put("phone",3);     //3表示正常
        return map;
    }

}
